<?php

class KolSys_Curl_Header
{
	private $cookiearr=array();
	private $lastLocation;
	public static function getInstance()
	{
		return KolSys_Singleton::getInstance(__CLASS__);
	}

	public static function read_header($ch, $string)
	{	//Usage: curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
		//This function is called every time a header is set
		global $settings;
		$t=KolSys_Singleton::getInstance(__CLASS__);
		$h=array('cok'=>'Set-Cookie:','loc'=>'Location:');
		if(strncmp($string, $h['cok'], strlen($h['cok']))==0)//meaning we have a cookie settings header
		{ #get the cookie
			//echo $string.'<hr>';
			$cookiestr = trim(substr($string, strlen($h['cok']), -1));
			$cookie = explode(';', $cookiestr);
			$cookie = explode('=', $cookie[0]);
			$cookiename = trim(array_shift($cookie));
			$t->cookiearr[$cookiename] = trim(implode('=', $cookie));

			if(preg_match('/Set-Cookie:\s*(.*)\b/', $string,$matches)){
				$cookieInfo = (array)http_parse_cookie($matches[1]);
				foreach($cookieInfo['cookies'] as $cookiename=>$cookievalue)
				{
					/*
					$e=mysql_real_escape_string($cookieInfo['expires']);
					$p=mysql_real_escape_string($cookieInfo['path']);
					$d=mysql_real_escape_string($cookieInfo['domain']);
					$e=$e?"FROM_UNIXTIME('$e')":'NULL';
					$p=$p?"'$p'":'NULL';
					$d=$d?"'$d'":'NULL';
					$cn=mysql_real_escape_string($cookiename);
					$cv=mysql_real_escape_string($cookievalue);
					if($cookievalue=='deleted'||$cookievalue==""||$cookievalue=="false"||$cookievalue=='FALSE'){
					$cv='NULL';
					}
					$un=mysql_real_escape_string($settings['kol']['username']);
					$sql="SELECT COUNT(*) FROM `cookies` WHERE `username`='$un' AND `cookiename`='$cn';";
					$query=mysql_query($sql) or die(mysql_error());
					list($count)=mysql_fetch_row($query);
					if($count){
						$sql="UPDATE `cookies` SET `cookievalue`='$cv',`expires`=$e,`path`=$p,`domain`=$d WHERE `username`='$un' AND `cookiename`='$cn';";
						$query=mysql_query($sql) or die(mysql_error());					
					}else{
						$sql="INSERT INTO `cookies` (`username`,`cookiename`,`cookievalue`,`expires`,`path`,`domain`) VALUES('$un','$cn','$cv',$e,$p,$d);";
						$query=mysql_query($sql) or die(mysql_error());
					}
					*/
				}
			}else{
				//echo 'dur';
			}
			
		}else if(strncmp($string, $h['loc'], strlen($h['loc']))==0){//meaning we have a redirect
			$locationstr = trim(substr($string, strlen($h['loc']), -1));
			$t->lastLocation=$locationstr;
		}
	
		return strlen($string);//header length
	}//function

	public static function getCookie()
	{
		$t=KolSys_Singleton::getInstance(__CLASS__);
		$cookiearr=$t->cookiearr;
		$cookieA = array();
		foreach ($cookiearr as $key=>$value)
		{
			if($value=='deleted'||$value==""||$value=="false"||$value=='FALSE')continue;//cookies set to 'deleted' the browser is supposed to remove
			$cookieA[]="$key=$value";
		}
		$cookie=implode(' ;',$cookieA);
		return $cookie;
	}

	public static function setCookieArray($value)
	{
		$t=KolSys_Singleton::getInstance(__CLASS__);
		$t->cookiearr=$value;
	}

	public static function setCookie($value)
	{
		$t=KolSys_Singleton::getInstance(__CLASS__);
		$t->cookiearr=array();
		$cookies = explode(';', $value);
		foreach($cookies as $cookie){
			list($cookiename,$cookievalue) = explode('=', $cookie);
			$t->cookiearr[trim($cookiename)] = trim($cookievalue);
		}
	}

	public static function getCookieArray()
	{
		$t=KolSys_Singleton(__CLASS__);
		return $t->cookiearr;
	}
	public static function getLastLocation()
	{
		$t=KolSys_Singleton(__CLASS__);
		return $t->lastLocation;
	}

}